package fschmidt.util.java;

import java.util.Map;


public final class SimpleCache<A,V> implements Computable<A,V> {
	private final Map<A,V> map;
	private final Computable<A,V> comp;

	public SimpleCache(Map<A,V> map,Computable<A,V> comp) {
		this.map = map;
		this.comp = comp;
	}

	public synchronized V get(A arg) throws ComputationException {
		V val = map.get(arg);
		if( val == null ) {
			try {
				val = comp.get(arg);
			} catch(RuntimeException e) {
				throw e;
			} catch(Exception e) {
				throw new ComputationException(e);
			}
			map.put(arg,val);
		}
		return val;
	}

	public synchronized void remove(A arg) {
		map.remove(arg);
	}

}
